package com.richdown.movie.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 对报文进行加密和编码或者解密和解码
 * 
 * 类 <code>Crypt</code>
 * 
 * @author wangwenyao@feinno.com
 * @version 2010-6-23
 */
@Repository
public class Crypt {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(Crypt.class);
	@Autowired
	private DESPlus DESPlus;
	/**
	 * 对报文进行加密，加密失败返回-1
	 * 
	 * @param content
	 *            报文
	 * @return
	 */
	public  String encrypt(String content) {
		String encoded = "-1";
		try {
//			DESTools des = DESTools.getInstance(SystemConfig.DES_KEY);
//			byte[] decoded = des.encrypt(content.getBytes());
//			encoded = URLEncoder.encode(Base64.encode(decoded), "UTF-8");
			encoded = DESPlus.encrypt(content);
		} catch (Exception e) {
			LOGGER.error("加密失败" + e.getMessage());
		}
		return encoded;
	}

	/**
	 * 对加密报文进行解密，解密失败范围-1
	 * 
	 * @param content
	 *            加密报文
	 * @return
	 */
	public  String decrypt(String content) {
		String encoded = "-1";
		try {
//			DESTools des = DESTools.getInstance(SystemConfig.DES_KEY);
//			byte[] decode = Base64.decode(URLDecoder.decode(content, "UTF-8"));
//			encoded = new String(des.decrypt(decode));
			encoded = DESPlus.decrypt(content);
		} catch (Exception e) {
			//encoded = content;
			LOGGER.error("解密失败" + e.getMessage());
		}
		return encoded;
	}

}
